﻿Imports TBCC.UtilityMobile
Imports System.Text.RegularExpressions

Public Class ProjectBasicInfoForm

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Me.Text = "增加终端"
    End Sub

    Public Sub New(ByVal pi As ProjectItem)
        MyClass.New()

        _pi = pi

        Me.Text = "修改终端"
    End Sub

    Private _pi As ProjectItem = Nothing
    Private Const code As String = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"

    Private Sub ProjectBasicInfoForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'For Each s As String In ProjectTypeList
        '    Me.cboProjectType.Items.Add(s)
        'Next

        'Me.cboProjectType.SelectedIndex = 0

        If _pi IsNot Nothing Then
            LoadInfo()
        End If
    End Sub

    Private Sub LoadInfo()
        'Me.cboProjectType.SelectedIndex = _pi.ProjectType - 1
        If _pi.ProjectType > 0 Then
            Me.labProjectType.Text = ProjectTypeList(_pi.ProjectType - 1)
        Else
            Me.labProjectType.Text = "未知"
        End If
        Me.txtProjectName.Text = _pi.ProjectName
        Me.txtRemark.Text = _pi.Remark
    End Sub


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If CheckValidate() = False Then
            Exit Sub
        End If

        Dim pi As ProjectItem = Nothing

        If _pi Is Nothing Then
            pi = New ProjectItem()
        Else
            pi = _pi
        End If

        pi.ProjectName = Me.txtProjectName.Text.Trim
        'pi.ProjectType = Me.cboProjectType.SelectedIndex + 1
        pi.Remark = Me.txtRemark.Text.Trim

        Dim flag As Boolean
        If _pi Is Nothing Then
            Try
                flag = ProjectService.CreateBasicInfo(pi)
                If flag = True Then
                    Dim dti As DevTypeItem = CreateDevTypeItem(pi)
                    ProjectService.AddDevRefTypeList(dti, 0, pi.ProjectType)
                End If
            Catch ex As Exception
                msg.ShowError(ex.Message)
                Exit Sub
            End Try

            msg.Show("增加终端成功。")

        Else
            Try
                flag = ProjectService.UpdateBasicInfo(pi)
                If flag = True Then
                    Dim dti As DevTypeItem = CreateDevTypeItem(pi)
                    ProjectService.UpdateDevRefTypeList(dti, pi)
                End If
            Catch ex As Exception
                msg.ShowError(ex.Message)
                Exit Sub
            End Try

            msg.Show("修改终端成功。")

        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub

    Public Shared Function CreateDevTypeItem(ByVal pi As ProjectItem) As DevTypeItem
        Dim dti As New DevTypeItem()
        dti.ProjectID = pi.ProjectID
        dti.NetId = 1
        dti.DevType = 3
        dti.RefNum = 1

        Dim rti As New RefTypeItem()
        rti.ProjectID = dti.ProjectID
        rti.NetId = dti.NetId
        rti.RefType = 1
        rti.RefId = 1
        rti.RealRefId = 1
        rti.FloorType = 1
        rti.FloorNo = 1
        rti.RefName = pi.ProjectName
        dti.RefTypeGroupList.Add(rti)

        Dim TAiPortCount, RHAiPortCount, DiDoPortCount As Integer
        Select Case pi.ProjectType
            Case ProjectType_Enum.Car '移动车载终端
                TAiPortCount = 3
                RHAiPortCount = 1
                DiDoPortCount = 2
            Case ProjectType_Enum.Box '小批零终端
                TAiPortCount = 1
                RHAiPortCount = 0
                DiDoPortCount = 0
            Case Else
                TAiPortCount = 0
                RHAiPortCount = 0
                DiDoPortCount = 0
        End Select
        For i As Integer = 1 To TAiPortCount
            Dim apti As New AiPortTypeItem()
            apti.ProjectID = rti.ProjectID
            apti.NetId = rti.NetId
            apti.RefId = rti.RefId
            apti.SeqPortNo = i
            apti.PortNo = i
            apti.PortName = "T" & i
            apti.DataType = 1
            rti.AiPortTypeGroupList.Add(apti)
        Next
        For i As Integer = 1 To RHAiPortCount
            Dim apti As New AiPortTypeItem()
            apti.ProjectID = rti.ProjectID
            apti.NetId = rti.NetId
            apti.RefId = rti.RefId
            apti.SeqPortNo = i
            apti.PortNo = TAiPortCount + i
            apti.PortName = "RH" & i
            apti.DataType = 2
            rti.AiPortTypeGroupList.Add(apti)
        Next
        For i As Integer = 1 To DiDoPortCount
            Dim dipti As New DiDoPortTypeItem()
            dipti.ProjectID = rti.ProjectID
            dipti.NetId = rti.NetId
            dipti.RefId = rti.RefId
            dipti.SeqPortNo = i
            dipti.PortType = 1
            dipti.PortNo = i
            dipti.PortName = "DI" & i
            dipti.DataType = 1
            Dim dopti As New DiDoPortTypeItem()
            dopti.ProjectID = rti.ProjectID
            dopti.NetId = rti.NetId
            dopti.RefId = rti.RefId
            dopti.SeqPortNo = i
            dopti.PortType = 2
            dopti.PortNo = i
            dopti.PortName = "DO" & i
            dopti.DataType = 1
            rti.DiDoPortTypeGroupList.Add(dopti)
        Next

        Return dti
    End Function

    Private Function CheckValidate() As Boolean
        If Me.txtProjectName.Text.Trim = "" Then
            msg.Show("终端名称不能为空。")
            Me.txtProjectName.Focus()
            Return False
        End If

        'If Me.cboProjectType.SelectedIndex < 0 Then
        '    msg.Show("终端类型未选择。")
        '    Me.cboProjectType.Focus()
        '    Return False
        'End If

        Return True
    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Function GetShowAuthCode(ByVal code As String) As String
        Dim ret As String = String.Empty
        Dim length As Integer = code.Length / 4 - 1

        For i As Integer = 0 To length
            ret &= code.Substring(i * 4, 4)
            If i < length Then
                ret &= "-"
            End If
        Next

        Return ret
    End Function

    Private Sub cboProjectType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'Me.txtProjectName.Text = Me.cboProjectType.SelectedItem.ToString()

    End Sub

End Class